/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.platform.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.dto.EquipmentLogManagementPageDTO;
import org.springblade.modules.platform.entity.EquipmentLogManagement;

import org.springblade.modules.platform.mapper.EquipmentLogManagementMapper;
import org.springblade.modules.platform.service.IEquipmentLogManagementService;
import org.springblade.modules.platform.vo.EquipmentLogManagementPageVO;
import org.springblade.modules.platform.vo.EquipmentLogManagementVO;
import org.springblade.modules.platform.vo.enums.LogTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-11-05
 */
@Service
public class EquipmentLogManagementServiceImpl extends ServiceImpl<EquipmentLogManagementMapper, EquipmentLogManagement> implements IEquipmentLogManagementService {

	@Override
	public IPage<EquipmentLogManagementVO> selectEquipmentLogManagementPage(IPage<EquipmentLogManagementVO> page, EquipmentLogManagementVO equipmentLogManagement) {
		return page.setRecords(baseMapper.selectEquipmentLogManagementPage(page, equipmentLogManagement));
	}

	@Override
	public EquipmentLogManagementPageVO selectEquipmentLogManagementByPage(EquipmentLogManagementPageDTO equipmentLogManagementPageDTO) {
		Integer curPage = ((equipmentLogManagementPageDTO.getCurrent()==null
			|| equipmentLogManagementPageDTO.getCurrent()==0?1:equipmentLogManagementPageDTO.getCurrent())-1) * (equipmentLogManagementPageDTO.getSize()==null?10:equipmentLogManagementPageDTO.getSize());
		equipmentLogManagementPageDTO.setCurrent(curPage);
		Integer integer = baseMapper.selectEquipmentLogManagementCount(equipmentLogManagementPageDTO);
		EquipmentLogManagementPageVO equipmentLogManagementPageVO = new EquipmentLogManagementPageVO();
		equipmentLogManagementPageVO.setTotal(integer);

		if(integer >0 ){
			List<EquipmentLogManagement> equipmentLogManagements = baseMapper.selectEquipmentLogManagementByPage(equipmentLogManagementPageDTO);
			if(!CollectionUtils.isEmpty(equipmentLogManagements)){
				List<EquipmentLogManagementVO> collect = equipmentLogManagements.stream().map(x -> {
					EquipmentLogManagementVO equipmentLogManagementVO = new EquipmentLogManagementVO();
					BeanUtils.copyProperties(x, equipmentLogManagementVO);
					equipmentLogManagementVO.setLogTypeEnum(x.getLogType() != null ? LogTypeEnum.getEnum(x.getLogType()) : null);
					return equipmentLogManagementVO;
				}).collect(Collectors.toList());
				equipmentLogManagementPageVO.setLogManagementVOS(collect);
			}
		}
		return equipmentLogManagementPageVO;

	}


}
